<?php

namespace app\index\controller;

use app\common\controller\Basecontroller;
use app\index\model\Company;
use app\index\model\Examine;
use app\index\model\Official;
use app\index\model\Region;
use app\index\model\User;
use think\Db;
use think\Request;

class SearchPage extends Basecontroller
{
    /**
     * @param Request $request
     * @param Official $official
     * @return string|\think\response\Json
     * ljj
     * 搜索接口(未完善)
     */
    public function searchOfficial(Request $request, Official $official)
    {
        $searchValue = $request->param('searchValue');
        $page = $request->param('page');
        $checkEducation = $request->param('searchEducation');
        $checkBackground = $request->param('searchBackground');
        $searchPeople = $request->param('searchPeople');
        $searchMoney = $request->param('searchMoney');
        $searchType = $request->param('searchType');
        $urban = $request->param('urban');
        $province = $request->param('province');
        $town = $request->param('town');
        $size = $request->param('size');
        if (empty($searchValue)) {
            $searchBack = $official->searchOfficialAllBack($page,$checkEducation, $checkBackground, $searchPeople,$searchMoney,$searchType,$province,$urban,$town);
            return $this->returnJson(self::OK ,'返回所有职位',$searchBack);
        }
        $searchCheck = json_encode($official->getSearchOfficialAttr($searchValue,$page, $checkEducation, $checkBackground, $searchPeople,$searchMoney,$searchType,$province,$urban,$town));
        if ($searchCheck == '[]') {
            return $this->returnJson(self::OK,'没有该内容');
        }else {
            $searchBackValue = $official->getSearchOfficialAttr($searchValue,$page, $checkEducation, $checkBackground, $searchPeople,$searchMoney,$searchType,$province,$urban,$town);
            return $this->returnJson(self::OK, '返回成功',$searchBackValue);
        }
    }
    public function searchCompany(Request $request,Company $company)
    {
        $searchValue = $request->param('searchValue');
        $page = $request->param('page');
        $searchType = $request->param('searchType');
        $urban = $request->param('urban');
        $province = $request->param('province');
        $town = $request->param('town');
        if (empty($searchValue)) {
            $searchBack = $company->getAllCompany($page,$searchType,$urban,$town,$province);
            return $this->returnJson(self::OK ,'返回所有公司',$searchBack);
        }
        $searchCheck = json_encode($company->checkSearch($searchValue,$page,$searchType,$urban,$town,$province));
        if ($searchCheck == '[]') {
            return $this->returnJson(self::OK,'没有该内容');
        }else {
            $searchBackValue = $company->getSearchCompanyAttr($searchValue,$page,$searchType,$urban,$town,$province);
            return $this->returnJson(self::OK, '返回成功',$searchBackValue);
        }
    }

    /**
     * @param Examine $examine
     * @return \think\response\Json
     * ljj
     * 返回热招职位信息
     */
    public function hotOfficial(Examine $examine)
    {
        return $this->returnJson(self::OK, '返回热招职位信息', $examine->hot());
    }
    public function hotCompany(Company $company)
    {
        return $this->returnJson(self::OK, '返回热们公司信息', $company->HotComBack());
    }
    public function areaBack(Region $region)
    {
        return $this->returnJson(self::OK,'返回省',$region->getReginAttr());
    }
    public function urbanBack(Request $request,Region $region)
    {
        $province = $request->param('province');
        $provinceCode = Db::table('region')->where('cri_name|cri_short_name','=',$province)->field('cri_code')->select();
        return $this->returnJson(self::OK,'返回市信息',$region->getUrbanAttr($provinceCode[0]['cri_code']));
    }
    public function townBack(Request $request,Region $region)
    {
        $urban = $request->param('urban');
        $urbanCode = Db::table('region')->where('cri_name|cri_short_name','=',$urban)->where('cri_level','=',2)->field('cri_code')->select();
        return $this->returnJson(self::OK,'返回市信息',$region->getTownAttr($urbanCode[0]['cri_code']));
    }

    public function hotSearch(Official $official)
    {
        return $this->returnJson(self::OK,'返回热门搜索',$official->hotSearch());
    }

    public function hotCity(Region $region)
    {
        return $this->returnJson(self::OK,'返回热门城市',$region->hotCity());
    }

    public function userHeader(User $user)
    {
        $userId = $this->getUserID();
        $header = $user->UserHeader($userId);
        return $this->returnJson(self::OK,'返回用户头像',$header);
    }
}